
package editor.map.elements
{
	import editor.common.Common;
	import editor.util.NumberTool;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	
	/**
	 * @author 缺硒
	 */
	public class Npc extends Sprite
	{
		public static var NpcBmd:BitmapData;
		
		[Embed(source="../../../assets/npc.png")]
		private var Bg:Class;
		private var _tileShow:Tile;
	
		private var _lp:Point;
		private var _npcId:String = "--";
		private var _isSelected:Boolean = false;
		
		private static var __filter:GlowFilter = new GlowFilter(0x00ffff, 1, 20, 20, 10);
		
		public var npcBmp:Bitmap;
		
		// 构造函数
		public function Npc()
		{
			if(NpcBmd == null)
			{
				NpcBmd = new Bg().bitmapData;
				NpcBmd.colorTransform(NpcBmd.rect, new ColorTransform(1,1,0));
			}
			npcBmp = new Bitmap(NpcBmd);
			npcBmp.x = - npcBmp.width / 2;
			npcBmp.y = - 90;
			npcBmp.alpha = 0.98;
			
			_tileShow = new Tile();
			_tileShow.draw(0x00ff00);
			_tileShow.x = -Tile.TILE_WIDTH / 2;
			_tileShow.y = -Tile.TILE_WIDTH / 2
			
			this.addChild(npcBmp);
			this.addChild(_tileShow);
			
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		public function get isSelected():Boolean
		{
			return _isSelected;
		}

		public function set isSelected(value:Boolean):void
		{
			_isSelected = value;
			
			if(_isSelected) this.filters = [__filter];
			else this.filters = null;
		}

		public function get npcId():String
		{
			return _npcId;
		}

		public function set npcId(value:String):void
		{
			_npcId = value;
		}

		/**
		 * 根据当前所在像素级的xy坐标来设置到逻辑位置
		 */		
		public function setLogic():void
		{
			_lp = NumberTool.p2lTransform(this.x, this.y);
			this.x = _lp.x * Tile.TILE_WIDTH + Tile.TILE_WIDTH / 2;
			this.y = _lp.y * Tile.TILE_WIDTH + Tile.TILE_WIDTH / 2;
		}
		
		private function onMouseDown(evt:MouseEvent):void
		{
			Common.ui.toolWindow.setSelectedNpc(this);
			this.startDrag();
		}
		
		private function onMouseUp(evt:MouseEvent):void
		{
			this.stopDrag();
		}
		//
	}
}